<template>
    <div class="login-container">
        <!-- 左侧背景区域 - 占2/3宽度 -->
        <div class="login-left">
            <div class="left-overlay"></div>
            <div class="left-content">
                <h1 class="system-name">运营支撑</h1>
                <p class="system-desc" style="margin-left: 165px;">高效管理，智能办公</p>
            </div>
        </div>
        
        <!-- 右侧登录区域 - 占1/3宽度 -->
        <div class="login-right">
            <div class="login-card">
                <h2 class="login-title">员工登录</h2>
                
                <el-form 
                    :model="ruleForm" 
                    status-icon 
                    :rules="rules" 
                    ref="ruleForm" 
                    label-width="0px"
                    class="login-form"
                >
                    <!-- 用户名输入框 -->
                    <el-form-item prop="userid" class="form-item">
                        <el-input 
                            type="text" 
                            v-model="ruleForm.userid" 
                            autocomplete="off"
                            placeholder="请输入用户名"
                            class="form-input"
                            @focus="handleInputFocus('userid')"
                            @blur="handleInputBlur('userid')"
                        >
                            <template #prefix>
                                <i class="el-icon-user input-icon"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    
                    <!-- 密码输入框 -->
                    <el-form-item prop="pwd" class="form-item">
                        <el-input 
                            :type="showPwd ? 'text' : 'password'" 
                            v-model="ruleForm.pwd" 
                            autocomplete="off"
                            placeholder="请输入密码"
                            class="form-input"
                            @focus="handleInputFocus('pwd')"
                            @blur="handleInputBlur('pwd')"
                        >
                            <template #prefix>
                                <i class="el-icon-lock input-icon"></i>
                            </template>
                            <template #suffix>
                                <i 
                                    class="el-icon-eye input-icon suffix-icon" 
                                    @click="showPwd = !showPwd"
                                    v-if="showPwd"
                                ></i>
                                <i 
                                    class="el-icon-eye-off input-icon suffix-icon" 
                                    @click="showPwd = !showPwd"
                                    v-else
                                ></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    
                    <!-- 忘记密码链接 -->
                    <div class="forgot-pwd">
                        <a href="javascript:;" @click="handleForgotPwd">忘记密码？</a>
                    </div>
                    
                    <!-- 提交按钮 -->
                    <el-form-item class="submit-item">
                        <el-button 
                            type="primary" 
                            @click="submitForm('ruleForm')"
                            class="login-btn"
                            :loading="isLoading"
                        >
                            <span v-if="!isLoading">登录</span>
                            <i class="el-icon-loading btn-loading" v-else></i>
                        </el-button>
                    </el-form-item>
                </el-form>
                
                
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'Login1',
    data() {
        return {
            ruleForm: {
                userid: '',
                pwd: ''
            },
            rules: {
                userid: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                ],
                pwd: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                ]
            },
            showPwd: false,
            isLoading: false,
            activeInput: ''
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.isLoading = true;
                    this.$http.post('/login', this.ruleForm)
                        .then((res) => {
                            this.isLoading = false;
                            if (res.data.code === 200) {
                                localStorage.setItem('userid', res.data.data.userid);
                                localStorage.setItem('empno', res.data.data.empno);
                                localStorage.setItem('roleid', res.data.data.roleid);
                                localStorage.setItem('ename', res.data.data.ename);
                                localStorage.setItem('deptno', res.data.data.deptno);
                                localStorage.setItem('jwt', res.data.jwt);
                                
                                this.$message({
                                    message: '登录成功',
                                    type: 'success'
                                });
                                this.$router.push({ path: '/menus' })
                                
                            } else {
                                this.$message({
                                    message: res.data.msg || '登录失败，请重试',
                                    type: 'error'
                                });
                            }
                        })
                        .catch((err) => {
                            this.isLoading = false;
                            console.log(err);
                            this.$message({
                                message: '网络异常，请检查连接',
                                type: 'error'
                            });
                        });
                } else {
                    console.log('请检查输入');
                    return false;
                }
            });
        },
        handleInputFocus(inputName) {
            this.activeInput = inputName;
        },
        handleInputBlur(inputName) {
            if (this.activeInput === inputName) {
                this.activeInput = '';
            }
        },
        handleForgotPwd() {
            this.$message({
                message: '请联系管理员重置密码',
                type: 'info'
            });
        }
    }
}
</script>

<style scoped>
/* 整体容器 - 左右分栏布局 */
.login-container {
    display: flex;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* 左侧区域 - 占2/3宽度 */
.login-left {
    width: 75%;
    height: 100%;
    background-image: url('../../../public/background.jpg');
    background-size: cover;
    background-position: center;
    position: relative;
}

/* 左侧背景遮罩 - 增强文字可读性 */
.left-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
}

/* 左侧内容 - 居中显示 */
.left-content {
    position: relative;
    z-index: 10;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 80px;
    color: white;
}

.system-name {
    font-size: 42px;
    font-weight: 700;
    margin-bottom: 20px;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.system-desc {
    font-size: 18px;
    max-width: 600px;
    line-height: 1.6;
    text-shadow: 0 1px 5px rgba(0, 0, 0, 0.3);
}

/* 右侧区域 - 占1/3宽度 */
.login-right {
    width: 25%;
    height: 100%;
    background-color: #f8fafc;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 40px;
}

/* 登录卡片 */
.login-card {
    width: 100%;
    max-width: 360px;
    background-color: white;
    padding: 40px 30px;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

/* 登录标题 */
.login-title {
    text-align: center;
    font-size: 22px;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 35px;
    padding-bottom: 15px;
    border-bottom: 1px solid #f1f5f9;
}

/* 表单样式 */
.login-form {
    width: 100%;
}

.form-item {
    margin-bottom: 20px;
}

/* 输入框样式 */
.form-input {
    height: 46px;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    font-size: 14px;
    transition: all 0.2s ease;
}

/* 输入框聚焦状态 */
.form-input:focus,
.activeInput.userid .form-input,
.activeInput.pwd .form-input {
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* 输入框图标 */
.input-icon {
    color: #94a3b8;
    font-size: 16px;
}

.suffix-icon {
    cursor: pointer;
    transition: color 0.2s;
}

.suffix-icon:hover {
    color: #3b82f6;
}

/* 忘记密码链接 */
.forgot-pwd {
    text-align: right;
    margin-top: -10px;
    margin-bottom: 25px;
}

.forgot-pwd a {
    font-size: 13px;
    color: #3b82f6;
    text-decoration: none;
    transition: color 0.2s;
}

.forgot-pwd a:hover {
    color: #2563eb;
    text-decoration: underline;
}

/* 登录按钮 */
.login-btn {
    width: 100%;
    height: 46px;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 500;
    background-color: #3b82f6;
    border: none;
    transition: all 0.2s;
}

.login-btn:hover {
    background-color: #2563eb;
    transform: translateY(-1px);
}

.login-btn:active {
    transform: translateY(1px);
}

.btn-loading {
    font-size: 18px;
}

/* 底部版权信息 */
.login-footer {
    margin-top: 30px;
    text-align: center;
}

.login-footer p {
    font-size: 12px;
    color: #94a3b8;
    margin: 0;
}

/* 响应式调整 - 在小屏幕上自动堆叠 */
@media (max-width: 992px) {
    .login-container {
        flex-direction: column;
    }
    
    .login-left {
        width: 100%;
        height: 30vh;
        min-height: 200px;
    }
    
    .login-right {
        width: 100%;
        height: 70vh;
        padding: 40px;
    }
    
    .left-content {
        padding: 0 40px;
    }
    
    .system-name {
        font-size: 28px;
    }
    
    .system-desc {
        font-size: 14px;
    }
}
</style>
